/********************************************************************
*	Author: Andy Wu
*	Description: Source file to handle data from the data segments
*	Modifications:
*		-Created on 03/21/2011
*			
**********************************************************************/

#include <iostream>
using std::cerr;
using std::cout;
using std::cin;
using std::endl;

#include <cstdlib> // for exit function

#include "dsegment.h"
#include <cstring>
#include <stdio.h>
#include <sstream>
#include <string>
using std::string;
#include <bitset>
using std::bitset;

/*function to test functions by printing out the generated machine code*/
void dsegprinttest(AssemblerData *a);

//main function to check functions
int main()
	{
	AssemblerData a,*a_ptr=&a;


//	(*a).mc_data= dsegmc(a);
	dsegprinttest(a_ptr);
	return 0;
	}

/*function to test functions by printing out the generated machine code and other related values*/
void dsegprinttest(AssemblerData *a)
	{
	/*test cases for data*/
	string fn="fname";
	string d1=".data",
		d2=".word",d2_1="10,",d2_2="-14,",d2_3="30",
		d3=".text",d4=".global main",d5=".data",
		d6=".word",d6_1="10,",d6_2="-14,",d6_3="30",
		d7=".ascii",d7_1="\"Hello, world\"",
		d8=".byte",d8_1="0x01,",d8_2="0x03",
		d9=".space",d9_1="100";
	string lb1="val:",lb2="main:",lb3="val:",lb4="str:",lb5="num:",lb6="arr:";
	unsigned int l1=1,l2=2,/*l3=3,*/l4=4,l5=5,
			/*l6=6,l7=7,*/l8=8,l9=9,l10=10,
			l11=11,l12=12;
	/*storing test cases into vector*/
	vector<string> dirdatavec1,dirdatavec2,dirdatavec3,dirdatavec4,dirdatavec5,dirdatavec6,dirdatavec7,
			dirdatavec8,dirdatavec9,newdirvec;

	dirdatavec1.push_back(d1);
	dirdatavec2.push_back(d2);dirdatavec2.push_back(d2_1);dirdatavec2.push_back(d2_2);dirdatavec2.push_back(d2_3);
	dirdatavec3.push_back(d3);dirdatavec4.push_back(d4);dirdatavec5.push_back(d5);
	dirdatavec6.push_back(d6);dirdatavec6.push_back(d6_1);dirdatavec6.push_back(d6_2);dirdatavec6.push_back(d6_3);
	dirdatavec7.push_back(d7);dirdatavec7.push_back(d7_1);
	dirdatavec8.push_back(d8);dirdatavec8.push_back(d8_1);dirdatavec8.push_back(d8_2);
	dirdatavec9.push_back(d9);dirdatavec9.push_back(d9_1);

	//appending to dir vector in class
	a->dir_append(dirdatavec1,l1,fn);
	a->dir_append(dirdatavec2,l2,fn);
	a->dir_append(dirdatavec3,l4,fn);
	a->dir_append(dirdatavec4,l5,fn);
	a->dir_append(dirdatavec5,l8,fn);
	a->dir_append(dirdatavec6,l9,fn);
	a->dir_append(dirdatavec7,l10,fn);
	a->dir_append(dirdatavec8,l11,fn);
	a->dir_append(dirdatavec9,l12,fn);


	//printing out elements stored in dir vector in class to test if done correctly
	unsigned int num=0;
	for(int i=0;i<(int)a->dir_len();i++)
		{
		unsigned int inc=0;
		newdirvec=a->ret_dir(num);	//gets the vector stored into the class at index num
		for(int j=0;j<(int)newdirvec.size();j++)
			{
			if(j==0)
				{
				cout<<"stuff in newdirvec"<<num<<"=";
				}
			cout<<newdirvec[j]<<" ";
			inc++;
			if((inc==newdirvec.size()))
				{
				cout<<endl;
				cout<<"\ta.ret_dirLN()="<<a->ret_dirLN(num)<<endl;
				}
			}
		num++;
		}

	/* Testcase came from this:
	   .data  # Tells assembler we're in the data segment
	val:  .word  10, -14, 30   # Three words placed in memory
	
	   .text  # Tells assembler we're in the text segment
	   .global main # Tells assembler main is accessible outside file
	main:  addi $sp, $sp, -8
	
	   .data  # Tells assembler we're in the data segment
	val:  .word  10, -14, 30   
	str:  .ascii  "Hello, world"
	num:  .byte  0x01, 0x03
	arr:  .space 100
	*/

	/*testing decnumtobin function*/
	int x;
	cout<<"Enter number(between 0 and 255): ";
	cin>>x;
	string s=decnumtobin(x);
	cout<<"Decimal_to_Bin version of "<<x<<" is "<<s<<endl;

	/*testing conversion from string to binary*/
	string str,bin;
//	cout<<"Enter string: ";
//	getline(cin,str);	//doesn't seem to be functional when decnumtobin testing is used before it
	bin=strtobinconv(s);
	cout<<"String_to_Bin version of "<<s<<" is "<<bin<<endl;

	/*retesting decnumtobin function*/
	cout<<"Enter number(between 0 and 255): ";
	cin>>x;
	s=decnumtobin(x);
	cout<<"Decimal_to_Bin version of "<<x<<" is "<<s<<endl;

	/*testing conversion from string to binary*/
//	cout<<"Enter string: ";
//	getline(cin,str);	//doesn't seem to be functional when decnumtobin testing is used before it
	bin=strtobinconv(s);
	cout<<"String_to_Bin version of "<<s<<" is "<<bin<<endl;

	/*testing decnumtobin function*/
	cout<<"Enter number(between 0 and 255): ";
	cin>>x;
	s=decnumtobin(x);
	cout<<"Decimal_to_Bin version of "<<x<<" is "<<s<<endl;

	/*testing conversion from string to binary*/
//	cout<<"Enter string: ";
//	getline(cin,str);	//doesn't seem to be functional when decnumtobin testing is used before it
	bin=strtobinconv(s);
	cout<<"String_to_Bin version of "<<s<<" is "<<bin<<endl;

	/*testing decnumtobin function*/
	cout<<"Enter number(between 0 and 255): ";
	cin>>x;
	s=decnumtobin(x);
	cout<<"Decimal_to_Bin version of "<<x<<" is "<<s<<endl;

	/*testing conversion from string to binary*/
//	cout<<"Enter string: ";
//	getline(cin,str);	//doesn't seem to be functional when decnumtobin testing is used before it
	bin=strtobinconv(s);
	cout<<"String_to_Bin version of "<<s<<" is "<<bin<<endl;
	}

